package com.kede.app.rest;

import com.kede.annotation.Log;
import com.kede.app.domain.LeescoreGoodsType;
import com.kede.app.ienum.TypeLevel;
import com.kede.app.service.LeescoreGoodsTypeService;
import com.kede.app.service.dto.LeescoreGoodsTypeQueryCriteria;
import com.kede.exception.BadRequestException;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import javax.servlet.http.HttpServletResponse;

/**
* @author Pr.li
* @date 2023-05-23
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "leescoreGoodType管理")
@RequestMapping("/api/leescoreGoodsType")
public class LeescoreGoodsTypeController {

    private final LeescoreGoodsTypeService leescoreGoodsTypeService;

    /**
     * 导出数据
     **/
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('leescoreGoodsType:list')")
    public void exportLeescoreGoodsType(HttpServletResponse response, LeescoreGoodsTypeQueryCriteria criteria) throws IOException {
        leescoreGoodsTypeService.download(leescoreGoodsTypeService.queryAll(criteria), response);
    }

    /**
    * 查询leescoreGoodType
    **/
    @GetMapping
    @Log("查询leescoreGoodType")
    @ApiOperation("查询leescoreGoodType")
    @PreAuthorize("@el.check('leescoreGoodsType:list')")
    public ResponseEntity<Object> queryLeescoreGoodsType(LeescoreGoodsTypeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(leescoreGoodsTypeService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    /**
     * 查询子选项
     **/
    @GetMapping("querySlaveAll")
    @Log("查询leescoreGoodType")
    @ApiOperation("查询leescoreGoodType")
    @PreAuthorize("@el.check('leescoreGoodsType:list')")
    public ResponseEntity<Object> querySlaveLeescoreGoodsType(Integer pid){
        return new ResponseEntity<>(leescoreGoodsTypeService.querySlaveAll(pid),HttpStatus.OK);
    }
    /**
    * 新增leescoreGoodType
    **/
    @PostMapping
    @Log("新增leescoreGoodType")
    @ApiOperation("新增leescoreGoodType")
    @PreAuthorize("@el.check('leescoreGoodsType:add')")
    public ResponseEntity<Object> createLeescoreGoodsType(@Validated @RequestBody LeescoreGoodsType resources){
        if (resources.getType().equals(TypeLevel.SLAVE.getValue()) && resources.getPid()==null) {
            throw new BadRequestException("子选项的pid  不可为空");
        }
        long l = LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8));
        return new ResponseEntity<>(leescoreGoodsTypeService.create(resources),HttpStatus.CREATED);
    }


    /**
    * 修改leescoreGoodType
    **/
    @PutMapping
    @Log("修改leescoreGoodType")
    @ApiOperation("修改leescoreGoodType")
    @PreAuthorize("@el.check('leescoreGoodsType:edit')")
    public ResponseEntity<Object> updateLeescoreGoodsType(@Validated @RequestBody LeescoreGoodsType resources){
        if (resources.getType().equals(TypeLevel.SLAVE.getValue()) && resources.getPid()==null) {
            throw new BadRequestException("子选项的pid  不可为空");
        }
        leescoreGoodsTypeService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    /**
    * 删除leescoreGoodType
    **/
    @DeleteMapping
    @Log("删除leescoreGoodType")
    @ApiOperation("删除leescoreGoodType")
    @PreAuthorize("@el.check('leescoreGoodsType:del')")
    public ResponseEntity<Object> deleteLeescoreGoodsType(@RequestBody Integer[] ids) {
        leescoreGoodsTypeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    /**
     * 获取主标签
     **/
    @GetMapping("getMasterLable")
    public ResponseEntity<Object> getMasterLable(){
        return new ResponseEntity(leescoreGoodsTypeService.getMasterLable(),HttpStatus.OK);
    }

}